package com.cls.expert.controller;

import com.cls.business.entity.*;
import com.cls.business.service.*;
import com.cls.business.vo.DeclareTopicVO;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.MyConstant;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.common.utils.MyUtil;
import com.cls.common.utils.StringUtils;
import com.cls.system.entity.Expert;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Controller
@RequiredArgsConstructor
public class ExpertViewController extends BaseController {
    private final IDeclareTopicService declareTopicService;
    private final ITopicService topicService;
    private final IDeclarePlanService declarePlanService;
    private final IReviewService reviewService;
    private final IClassDistributionRecordService recordService;

    /**
     * 新增申报页面的新增项目信息 addReview
     *
     * @return a
     * @author Carey
     * @description
     * @date 2020/11/11 3:43 下午
     */
    @RequestMapping(MyConstant.VIEW_PREFIX + "expert/addReview/{id}/{reviewId}")
    public String addReview(Model model, @PathVariable String id, @PathVariable String reviewId) {
        DeclareTopic declareTopic = declareTopicService.getById(id);
        DeclareTopicVO declareTopicVO = new DeclareTopicVO(declareTopic);
        setData(model, declareTopic, declareTopicVO, reviewId);
        model.addAttribute("result", declareTopicVO);
        model.addAttribute("id", id);
        model.addAttribute("reviewId", reviewId);
        return MyUtil.view("review/addReview");
    }


    /**
     * 申报表的查看页面跳转
     *
     * @return a
     * @author Carey
     * @description
     * @date 2020/11/11 3:43 下午
     */
    @RequestMapping(MyConstant.VIEW_PREFIX + "expert/classReviewDetail/{id}/{reviewId}")
    public String classReviewDetail(Model model, @PathVariable String id, @PathVariable String reviewId) {
        DeclareTopic declareTopic = declareTopicService.getById(id);


        DeclareTopicVO declareTopicVO = new DeclareTopicVO(declareTopic);
        if (Objects.nonNull(declareTopicVO.getTopicId())) {
            Topic byId = topicService.getById(declareTopicVO.getTopicId());
            declareTopicVO.setTopic(byId);
        }

        setData(model, declareTopic, declareTopicVO, reviewId);
        model.addAttribute("result", declareTopicVO);
        model.addAttribute("id", id);
        model.addAttribute("reviewId", reviewId);

        return MyUtil.view("review/classReviewDetail");
    }


    /**
     * 专家评审表分页查询
     *
     * @return a
     * @author Carey
     * @description
     * @date 2020/11/11 3:43 下午
     */
    @GetMapping("expert/guide/list/{planId}")
    @ResponseBody
    public ResponseVO reviewGuideList(QueryRequest request, Expert expert, @PathVariable Long planId) {
        PageInfo<Expert> pageInfo = recordService.findClassDistributionRecordsByExpertId(request, expert, planId);
        return ResponseVO.success(pageInfo);
    }

    /**
     * @return a
     * @author Carey
     * @description
     * @date 2020/11/11 3:43 下午
     */
    @GetMapping("expert/guide/review/list")
    @ResponseBody
    public ResponseVO classGuideList(QueryRequest request, ClassDistributionRecord classDistributionRecord) {
        PageInfo<ClassDistributionRecord> pageInfo = recordService.findRecordsAndYearByExpertId(request, classDistributionRecord);
        return ResponseVO.success(pageInfo);
    }


    /**
     * @return a
     * @author Carey
     * @description
     * @date 2020/11/11 3:43 下午
     */
    @GetMapping("expert/guide/review/finished")
    @ResponseBody
    public ResponseVO classGuideFinished(Long id) {
        ClassDistributionRecord record = recordService.getById(id);
        if (StringUtils.isNotBlank(record.getReviewText())) {
//            不为空代表评审文字有保存，表示可以提交
            return ResponseVO.success(true);
        }

        return ResponseVO.success(false);
    }


    /**
     * 新增申报页面的新增项目信息 addReview
     *
     * @return a
     * @author Carey
     * @description
     * @date 2020/11/11 3:43 下午
     */
    @RequestMapping(MyConstant.VIEW_PREFIX + "expert/annualReview/{distributionRecordId}/{year}")
    public String annualReview(Model model, @PathVariable Long distributionRecordId, @PathVariable Long year) {
        model.addAttribute("distributionRecordId", distributionRecordId);
        model.addAttribute("year", year);
        ClassDistributionRecord record = recordService.getById(distributionRecordId);
        model.addAttribute("record", record);
        return MyUtil.view("review/annualReviewDetail");
    }

    public void setData(Model model, DeclareTopic declareTopic, DeclareTopicVO declareTopicVO, String reviewId) {
        //评审意见
        Review review = reviewService.getById(reviewId);
        //查询成果形式和字数
        DeclarePlan declarePlan = new DeclarePlan();
        declarePlan.setDeclareId(declareTopic.getDeclareId());
        declarePlan.setPlanId(review.getPlanId());
        List<DeclarePlan> declarePlans = declarePlanService.findDeclarePlans(declarePlan);
        if (declarePlans != null && declarePlans.size() > 0) {
            DeclarePlan declarePlanGet = declarePlans.get(0);
            Optional.ofNullable(declarePlanGet.getAchievementFormTypeCode()).ifPresent(declareTopicVO::setAchievementFormTypeCode);
            Optional.ofNullable(declarePlanGet.getNumber()).ifPresent(declareTopicVO::setAchievementFormSize);
            Optional.ofNullable(declarePlanGet.getFileHideId()).ifPresent(declareTopicVO::setAnonymousFileId);
        }
        model.addAttribute("opinion", review.getOpinion());
    }
}
